package xyz.kongbai121.modules.sys.controller;

import xyz.kongbai121.common.utils.PageUtils;
import xyz.kongbai121.common.utils.Result;
import xyz.kongbai121.modules.sys.entity.DictType;
import xyz.kongbai121.modules.sys.entity.SysDictDataEntity;
import xyz.kongbai121.modules.sys.service.SysDictDataService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 字典数据
 *
 * @author ljq
 * @date 2021-11-08 17:36:53
 */
@Api(tags = "字典数据管理")
@RestController
@RequestMapping("/sys/sysdictdata")
public class SysDictDataController {
    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    @RequiresPermissions("sys:sysdictdata:list")
    public Result<PageUtils> list(@ApiParam(value = "当前页码", example = "0", required = false) @RequestParam(value = "curPage") Long curPage,
                                  @ApiParam(value = "每页显示条数", example = "0", required = false) @RequestParam(value = "pageSize") Long pageSize,
                                  @ApiParam(value = "父类类型ID", example = "1", required = true) @RequestParam Long dictTypeId) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", curPage);
        params.put("limit", pageSize);
        params.put("dictTypeId", dictTypeId);
        PageUtils page = sysDictDataService.queryPage(params);

        return Result.ok(page);
    }

    /**
     * 详情
     */
    @ApiOperation(value = "详情")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:sysdictdata:info")
    public Result<SysDictDataEntity> info(@ApiParam(value = "id", example = "0", required=true) @PathVariable("id") Long id){
		SysDictDataEntity sysDictData = sysDictDataService.getById(id);

        return Result.ok(sysDictData);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    @RequiresPermissions("sys:sysdictdata:save")
    public Result<?> save(@ApiParam(value = "实体", required=true) @RequestBody SysDictDataEntity sysDictData){

		sysDictDataService.save(sysDictData);

        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:sysdictdata:update")
    public Result<?> update(@ApiParam(value = "实体", required=true) @RequestBody SysDictDataEntity sysDictData){
		sysDictDataService.updateById(sysDictData);

        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @RequiresPermissions("sys:sysdictdata:delete")
    public Result<?> delete(@ApiParam(value = "id数组", required=true) @RequestBody Long[] ids){
		sysDictDataService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

    @ApiOperation("所有字典数据")
    @GetMapping("all")
    public Result<List<DictType>> all(){
        List<DictType> list = sysDictDataService.getAllList();
        return Result.ok(list);
    }

    @ApiOperation("根据字典类型获取字典数据")
    @GetMapping("dataByType")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictType", value = "字典类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictValue", value = "字典数值", paramType = "query", dataType = "int"),
    })
    @RequiresPermissions("sys:sysdictdata:page")
    public Result<List<SysDictDataEntity>> dataByType(@ApiIgnore @RequestParam Map<String, Object> params) {
        //字典类型
        List<SysDictDataEntity> data = sysDictDataService.dataByType(params);

        return Result.ok(data);
    }

    @ApiOperation("重新加载数据到Redis")
    @PostMapping("refresh")
    public Result<?> refresh() {
        return sysDictDataService.refresh();
    }

}
